package com.example.day02;


import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day02.p.P_Main;
import com.example.day02.v.V_view;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;


/**
 * A simple {@link Fragment} subclass.
 */
public class AlankFragment extends Fragment implements V_view, View.OnClickListener {


    private RecyclerView mRecy;
    private ArrayList<GetBean.ResultsBean> beans;
    private Recy_Adapter adapter;
    private EditText mEdit;
    private Button mButton;
    private Button mButton2;
    private PopupWindow popupWindow;

    public AlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_alank, container, false);
        P_Main p_main = new P_Main(this);
        p_main.getData();
        initView(inflate);
        return inflate;
    }
    private void initView(@NonNull final View itemView) {
        mRecy = (RecyclerView) itemView.findViewById(R.id.recy);
        mRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecy.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        beans = new ArrayList<>();
        adapter = new Recy_Adapter(getActivity(), beans);
         adapter.setOncreate(new Recy_Adapter.Oncreate() {

             @Override
        public void oncreate(int position) {
                 View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.item2, null);
                 popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT, false);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);
            popupWindow.showAtLocation(inflate, Gravity.CENTER,0,0);
            Button bt = inflate.findViewById(R.id.button);
            Button bt1 = inflate.findViewById(R.id.button2);
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            bt1.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onClick(View v) {
                    Notification build = new Notification.Builder(getActivity())
                            .setContentTitle("通知")
                            .setContentText("明天休息")
                            .setSmallIcon(R.drawable.d)
                            .build();
                    NotificationManager systemService = (NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);
                    systemService.notify(1,build);
                }
            });

             }
    });
        mRecy.setAdapter(adapter);
    }

    @Override
    public void showData(List<GetBean.ResultsBean> getlist) {
        beans.addAll(getlist);
        adapter.notifyDataSetChanged();
    }
    @Override
    public void showToasi(String str) {
        Toast.makeText(getActivity(), "str", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                break;
            case R.id.button2:
                break;
            default:
                break;
        }
    }
}

